package com.future.mayi.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.app.App;
import com.future.mayi.model.PalaceItemModel;
import com.future.mayi.model.PalaceModel;
import com.future.mayi.model.interfaces.IndexPalaceClickListener;
import com.future.mayi.utils.HCUtils;

import java.util.List;

public class IndexPalaceView extends LinearLayout implements View.OnClickListener {
    private Context mContext;
    private LayoutInflater layoutInflater;

    public IndexPalaceView(Context context) {
        super(context);
        init(context);
    }

    public IndexPalaceView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public IndexPalaceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        layoutInflater = LayoutInflater.from(context);
        setOrientation(VERTICAL);
    }


    private List<PalaceModel> palace;


    public void setPalace(List<PalaceModel> palace) {
        if (this.palace != null && palace != null) {
            if (TextUtils.equals(App.getGson().toJson(this.palace), App.getGson().toJson(palace))) {
                return;
            }
        }

        this.palace = palace;
        if (palace == null || palace.size() == 0) {
            removeAllViews();
            setVisibility(GONE);
            return;
        }
        setVisibility(VISIBLE);
        removeAllViews();
        for (PalaceModel palaceModel : palace) {
            if (palaceModel != null && palaceModel.getItem() != null && palaceModel.getItem().size() > 2) {
                View view = null;
                TextView tvOne = null;
                TextView tvSubOne = null;
                ImageView ivOne = null;
                TextView tvTwo = null;
                TextView tvSubTwo = null;
                ImageView ivTwo = null;
                TextView tvThree = null;
                TextView tvSubThree = null;
                ImageView ivThree = null;
                TextView tvFour = null;
                TextView tvSubFour = null;
                ImageView ivFour = null;
                View viewOne = null;
                View viewTwo = null;
                View viewThree = null;
                View viewFour = null;


                if (palaceModel.getItem().size() == 3) {
                    view = layoutInflater.inflate(R.layout.layout_index_head_three_item, this, false);
                    tvOne = (TextView) view.findViewById(R.id.tv_title_one);
                    tvSubOne = (TextView) view.findViewById(R.id.tv_sub_title_one);
                    ivOne = (ImageView) view.findViewById(R.id.iv_img_one);
                    tvTwo = (TextView) view.findViewById(R.id.tv_title_two);
                    tvSubTwo = (TextView) view.findViewById(R.id.tv_sub_title_two);
                    ivTwo = (ImageView) view.findViewById(R.id.iv_img_two);
                    tvThree = (TextView) view.findViewById(R.id.tv_title_three);
                    tvSubThree = (TextView) view.findViewById(R.id.tv_sub_title_three);
                    ivThree = (ImageView) view.findViewById(R.id.iv_img_three);
                    viewOne = view.findViewById(R.id.ll_one);
                    viewTwo = view.findViewById(R.id.ll_two);
                    viewThree = view.findViewById(R.id.ll_three);

                } else if (palaceModel.getItem().size() == 4) {
                    view = layoutInflater.inflate(R.layout.layout_index_head_four_item, this, false);
                    tvOne = (TextView) view.findViewById(R.id.tv_title_one);
                    tvSubOne = (TextView) view.findViewById(R.id.tv_sub_title_one);
                    ivOne = (ImageView) view.findViewById(R.id.iv_img_one);
                    tvTwo = (TextView) view.findViewById(R.id.tv_title_two);
                    tvSubTwo = (TextView) view.findViewById(R.id.tv_sub_title_two);
                    ivTwo = (ImageView) view.findViewById(R.id.iv_img_two);
                    tvThree = (TextView) view.findViewById(R.id.tv_title_three);
                    tvSubThree = (TextView) view.findViewById(R.id.tv_sub_title_three);
                    ivThree = (ImageView) view.findViewById(R.id.iv_img_three);
                    tvFour = (TextView) view.findViewById(R.id.tv_title_four);
                    tvSubFour = (TextView) view.findViewById(R.id.tv_sub_title_four);
                    ivFour = (ImageView) view.findViewById(R.id.iv_img_four);
                    viewOne = view.findViewById(R.id.ll_one);
                    viewTwo = view.findViewById(R.id.ll_two);
                    viewThree = view.findViewById(R.id.ll_three);
                    viewFour = view.findViewById(R.id.ll_four);
                }
                if (view != null) {
                    addView(view);
                    LinearLayout.LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
                    layoutParams.bottomMargin = CommonUtils.dip2px(mContext, 1);

                    if (viewOne != null && palaceModel.getItem().get(0) != null) {
                        palaceModel.getItem().get(0).setPalace_id(palaceModel.getPalace_id());
                        palaceModel.getItem().get(0).setPalace_name(palaceModel.getPalace_name());
                        viewOne.setTag(palaceModel.getItem().get(0));
                        viewOne.setOnClickListener(this);
                        tvOne.setText(palaceModel.getItem().get(0).getTitle());
                        tvSubOne.setText(palaceModel.getItem().get(0).getSubtitle());
                        HCUtils.loadWebImg(mContext, ivOne, palaceModel.getItem().get(0).getItem_img());
                    }

                    if (viewTwo != null && palaceModel.getItem().get(1) != null) {
                        palaceModel.getItem().get(1).setPalace_id(palaceModel.getPalace_id());
                        palaceModel.getItem().get(1).setPalace_name(palaceModel.getPalace_name());
                        viewTwo.setTag(palaceModel.getItem().get(1));
                        viewTwo.setOnClickListener(this);
                        tvTwo.setText(palaceModel.getItem().get(1).getTitle());
                        tvSubTwo.setText(palaceModel.getItem().get(1).getSubtitle());
                        HCUtils.loadWebImg(mContext, ivTwo, palaceModel.getItem().get(1).getItem_img());
                    }


                    if (viewThree != null && palaceModel.getItem().get(2) != null) {
                        palaceModel.getItem().get(2).setPalace_id(palaceModel.getPalace_id());
                        palaceModel.getItem().get(2).setPalace_name(palaceModel.getPalace_name());
                        viewThree.setTag(palaceModel.getItem().get(2));
                        viewThree.setOnClickListener(this);
                        tvThree.setText(palaceModel.getItem().get(2).getTitle());
                        tvSubThree.setText(palaceModel.getItem().get(2).getSubtitle());
                        HCUtils.loadWebImg(mContext, ivThree, palaceModel.getItem().get(2).getItem_img());
                    }


                    if (viewFour != null && palaceModel.getItem().size() > 3 && palaceModel.getItem().get(3) != null) {
                        palaceModel.getItem().get(3).setPalace_id(palaceModel.getPalace_id());
                        palaceModel.getItem().get(3).setPalace_name(palaceModel.getPalace_name());
                        viewFour.setTag(palaceModel.getItem().get(3));
                        viewFour.setOnClickListener(this);
                        tvFour.setText(palaceModel.getItem().get(3).getTitle());
                        tvSubFour.setText(palaceModel.getItem().get(3).getSubtitle());
                        HCUtils.loadWebImg(mContext, ivFour, palaceModel.getItem().get(3).getItem_img());
                    }


                }


            }
        }

    }

    @Override
    public void onClick(View v) {
        if (v.getTag() == null) return;
        if (!(v.getTag() instanceof PalaceItemModel)) return;
        PalaceItemModel model = (PalaceItemModel) v.getTag();
        if (model == null || listener == null) return;
        listener.onIndexPalaceItemClick(model);
    }

    private IndexPalaceClickListener listener;

    public void setListener(IndexPalaceClickListener listener) {
        this.listener = listener;
    }
}
